
# Store File (Store & Upload)
# Author: Rishav Jain

# importing variables from other project files
from profile import profile
import variables
from upload import uploadReadings

# import required libraries
import time
import os
import threading

# Task to Upload the data from files to SensorAct
UploadTask = threading.Thread(target = uploadReadings, args=())

def StoreTask():
    UploadTask.start()
    
    # saving current time
    prevTime = time.time()

    uploadCount = 0
    while(True):
        nowTime = time.time()

        if nowTime-prevTime > 1:
            prevTime = nowTime

            if variables.alarmCount == 0:
                variables.timestamp = nowTime
                print('Updating TimeStamp : ' + str(variables.timestamp))

            if variables.alarmCount % profile.SamplingPeriod == 0:
                print('alarmCount:' + str(variables.alarmCount), 'Sampling', time.asctime())

                if variables.alarmCount%2 == 0:
                    variables.TempReadings += (0.0,)
                    variables.LightReadings += (0.0,)
                    variables.PirReadings += (0,)

                else:
                    variables.TempReadings += (1.0,)
                    variables.LightReadings += (1.0,)
                    variables.PirReadings += (1,)

            variables.alarmCount += 1
            uploadCount += 1

            if variables.alarmCount % profile.StoreLength == 0:
                print('alarmCount:' + str(variables.alarmCount), 'Storing', time.asctime())
                variables.alarmCount = 0
                
                path = profile.StorePath + str(variables.timestamp) + '/'

                if not os.path.exists(path):
                    os.makedirs(path)
                    
                tempFile = open(path + variables.TEMP_TAG + '.csv', 'w+')
                print('TempReadings -')
                print(variables.TempReadings)
                tempFile.write(str(variables.TempReadings).strip()[1:len(str(variables.TempReadings))-1])
                variables.TempReadings=()
                tempFile.close()

                lightFile = open(path + variables.LIGHT_TAG + '.csv', 'w+')
                print('LightReadings -')
                print(variables.LightReadings)
                lightFile.write(str(variables.LightReadings).strip()[1:len(str(variables.LightReadings))-1])
                variables.LightReadings=()
                lightFile.close()

                pirFile = open(path + variables.PIR_TAG + '.csv', 'w+')
                print('PirReadings -')
                print(variables.PirReadings)
                pirFile.write(str(variables.PirReadings).strip()[1:len(str(variables.PirReadings))-1])
                variables.PirReadings=()
                pirFile.close()

            if uploadCount % profile.UploadPeriod == 0:
                print('uploadCount:' + str(uploadCount), 'Upload Initiated', time.asctime())
                variables.alarmUpload = 1
                uploadCount = 0

                

#For testing this module ONLY
#StoreTask()
